#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.views import View
from django.shortcuts import render, redirect, HttpResponse
from teach import models
from teach.forms.course import CourseModelForm, ModuleModelForm, SimpleDailyModelForm, DailyModelForm
from teach.utils.urls import advanced_reverse, advanced_url, advanced_origin_params


def course(request, *args, **kwargs):
    """
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    if request.method == "GET":
        daily_form = SimpleDailyModelForm()
    else:
        daily_form = SimpleDailyModelForm(request.POST)
        mid = request.GET.get('mid')
        module_obj = models.Module.objects.filter(id=mid).first()
        if not module_obj:
            daily_form.add_error('order', '请先选择模块')
        elif daily_form.is_valid():
            daily_form.instance.module = module_obj
            daily_form.save()
            return redirect(request.get_full_path())

    course_queryset = models.Course.objects.all()

    cid = request.GET.get('cid', 0)
    module_queryset = models.Module.objects.filter(course_id=cid).order_by('order') if cid else []

    mid = request.GET.get('mid', 0)
    if mid:
        daily_queryset = models.Daily.objects.filter(module_id=mid).order_by('module__order','order')
    elif cid:
        daily_queryset = models.Daily.objects.filter(module__course_id=cid).order_by('module__order','order')
    else:
        daily_queryset = []

    return render(
        request,
        'teach/course.html',
        {
            'course_queryset': course_queryset,
            'module_queryset': module_queryset,
            'daily_queryset': daily_queryset,
            'cid': cid,
            'mid': mid,
            'daily_form': daily_form
        }
    )


def course_add(request):
    """
    添加课程
    :param request:
    :return:
    """
    title = '添加课程'
    if request.method == 'GET':
        form = CourseModelForm()
    else:
        form = CourseModelForm(request.POST)
        if form.is_valid():
            form.save()
            if request.POST.get('_save'):
                return redirect(advanced_reverse(request, 'teach:course'))
            elif request.POST.get('_add_another'):
                return redirect(advanced_origin_params(request, 'teach:course_add'))

    return render(request, 'teach/change.html', {'form': form, 'title': title})


def course_edit(request, cid):
    """
    编辑课程
    :param request:
    :param cid:
    :return:
    """
    title = '编辑课程'
    obj = models.Course.objects.filter(id=cid).first()
    if not obj:
        return HttpResponse('课程不存在')

    if request.method == 'GET':
        form = CourseModelForm(instance=obj)
    else:
        form = CourseModelForm(request.POST, instance=obj)
        if form.is_valid():
            form.save()
            if request.POST.get('_save'):
                return redirect(advanced_reverse(request, 'teach:course'))
            elif request.POST.get('_add_another'):
                return redirect(advanced_origin_params(request, 'teach:course_add'))
    return render(request, 'teach/change.html', {'form': form, 'title': title})


def course_del(request, cid):
    """
    删除
    :param request:
    :param cid:
    :return:
    """
    list_url = advanced_reverse(request, 'teach:course')
    if request.method == 'GET':
        return render(request, 'teach/delete.html', {'cancel_url': list_url})

    models.Course.objects.filter(id=cid).delete()
    return redirect(list_url)


def module_add(request, cid):
    """
    添加模块
    :param request:
    :return:
    """
    course = models.Course.objects.filter(id=cid).first()
    if not course:
        return HttpResponse('课程不存在，请重新选择课程！')

    title = '添加模块'
    if request.method == 'GET':
        form = ModuleModelForm()
    else:
        form = ModuleModelForm(request.POST)
        if form.is_valid():
            form.instance.course = course
            form.save()
            if request.POST.get('_save'):
                return redirect(advanced_reverse(request, 'teach:course'))
            elif request.POST.get('_add_another'):
                return redirect(advanced_origin_params(request, 'teach:module_add', cid=cid))

    return render(request, 'teach/change.html', {'form': form, 'title': title})


def module_edit(request, cid, mid):
    """
    编辑课程
    :param request:
    :param mid:
    :return:
    """
    title = '编辑模块'
    obj = models.Module.objects.filter(id=mid, course_id=cid).first()
    if not obj:
        return HttpResponse('模块不存在，请重新选择模块！')

    if request.method == 'GET':
        form = ModuleModelForm(instance=obj)
    else:
        form = ModuleModelForm(request.POST, instance=obj)
        if form.is_valid():
            form.save()
            if request.POST.get('_save'):
                return redirect(advanced_reverse(request, 'teach:course'))
            elif request.POST.get('_add_another'):
                return redirect(advanced_origin_params(request, 'teach:module_add', cid=cid))
    return render(request, 'teach/change.html', {'form': form, 'title': title})


def module_del(request, cid, mid):
    """
    删除模块
    :param request:
    :param mid:
    :return:
    """

    list_url = advanced_reverse(request, 'teach:course')
    if request.method == 'GET':
        return render(request, 'teach/delete.html', {'cancel_url': list_url})

    models.Module.objects.filter(id=mid).delete()
    return redirect(list_url)


def daily_add(request, cid):
    """
    添加日计划
    :param request:
    :return:
    """
    title = '添加日计划'
    if request.method == 'GET':
        form = DailyModelForm(cid)
        return render(request, 'teach/daily_change.html', {'form': form, 'title': title})
    form = DailyModelForm(cid, request.POST)
    if form.is_valid():
        form.save()
        if request.POST.get('_save'):
            return redirect(advanced_reverse(request, 'teach:course'))
        elif request.POST.get('_add_another'):
            return redirect(advanced_origin_params(request, 'teach:daily_add', cid=cid))
    return render(request, 'teach/daily_change.html', {'form': form, 'title': title})


def daily_edit(request, cid, did):
    """
    编辑日计划
    :param request:
    :param did:
    :return:
    """
    title = '编辑日计划'
    obj = models.Daily.objects.filter(id=did).first()
    if not object:
        return HttpResponse('日计划不存在')
    if request.method == 'GET':
        form = DailyModelForm(cid, instance=obj)
        return render(request, 'teach/daily_change.html', {'form': form, 'title': title})
    form = DailyModelForm(cid, request.POST, instance=obj)
    if form.is_valid():
        form.save()
        if request.POST.get('_save'):
            return redirect(advanced_reverse(request, 'teach:course'))
        elif request.POST.get('_add_another'):
            return redirect(advanced_origin_params(request, 'teach:daily_add', cid=cid))
    return render(request, 'teach/daily_change.html', {'form': form, 'title': title})


def daily_del(request, cid, did):
    """
    删除日计划
    :param request:
    :param did:
    :return:
    """
    list_url = advanced_reverse(request, 'teach:course')
    if request.method == 'GET':
        return render(request, 'teach/delete.html', {'cancel_url': list_url})

    models.Daily.objects.filter(id=did).delete()
    return redirect(list_url)


def daily_prev(request, did):
    """
    课程预览
    :param request:
    :param did:
    :return:
    """
    obj = models.Daily.objects.filter(pk=did).first()
    return render(request, 'teach/daily_prev.html', {'obj': obj})
